package com.foshanshop.ejb3.service.impl;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.ejb.Init;
import javax.ejb.Local;
import javax.ejb.PostActivate;
import javax.ejb.PrePassivate;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

import com.b2besoft.po.TestDalDs;
import com.b2besoft.po.TestOracleDs;
import com.foshanshop.ejb3.dao.DAOFactory;
import com.foshanshop.ejb3.service.TestStateFulBean;
import com.foshanshop.ejb3.service.TestStateFulBeanRefer;
import com.foshanshop.ejb3.service.TestStateLessBean;

@Stateful
@Local({TestStateFulBean.class})
public class TestStateFulBeanImpl implements TestStateFulBean {
    
    @EJB
     TestStateFulBeanRefer stateFulBeanRefer;
    
    
    @EJB
    TestStateLessBean testStateLessBean;
    
   @TransactionAttribute( TransactionAttributeType.REQUIRED )
   public String saveDal(){
       TestDalDs   ds = new TestDalDs();
       ds.setName("test1");
       ds.setData("ok");
       DAOFactory.getDalDAO().save(ds);
       getDalCount();
        return "ok";
   }
    @TransactionAttribute( TransactionAttributeType.REQUIRED )
    public String saveOracle(){
        TestOracleDs   ds = new TestOracleDs();
        ds.setName("test1");
        ds.setData("ok");
         DAOFactory.getOracleDAO().save(ds);
         getOracleCount();
         return "ok";
    }
    @TransactionAttribute( TransactionAttributeType.REQUIRED )
    public String saveMutil(){
        saveDal();
        saveOracle();
        getDalCount();
        getOracleCount();
        System.out.println("自己执行完成，关联Bean开始执行");
        stateFulBeanRefer.saveMutil();
        System.out.println("无状态Bean的调用");
        testStateLessBean.getOracleCount();
        return "ok";
    }
    @TransactionAttribute( TransactionAttributeType.REQUIRED )
    public String saveDalError() {
       getDalCount();
        saveDal();
      getDalCount();
        int j = 3/0;
        return null;
    }
    @TransactionAttribute( TransactionAttributeType.REQUIRED )
    public String saveMutilErrorAll() {
        getDalCount();
        getOracleCount();
        saveMutil();
        getDalCount();
        getOracleCount();
        int j = 3/0;
        return null;
    }

    @TransactionAttribute( TransactionAttributeType.REQUIRED )
    public String saveOracleError() {
        getOracleCount() ;
        saveOracle();
        getOracleCount() ;
        int j = 3/0;
        return null;
    }

    @Init
    public void initialize () {
      System.out.println(this.getClass().getName() + "  initialize()方法被调用");
    }   
    
    @PostConstruct
    public void Construct () {
      System.out.println(this.getClass().getName() + "  Construct()方法被调用");
    }

    @PreDestroy
    public void exit () {
      System.out.println(this.getClass().getName() + "  exit()方法被调用");
    }

    @PrePassivate
    public void serialize () {
      System.out.println(this.getClass().getName() + "  serialize()方法被调用");
    }

    @PostActivate
    public void activate () {
      System.out.println(this.getClass().getName() + "  activate()方法被调用");
    }

    @Remove
    public void stopSession () {
      System.out.println(this.getClass().getName() + "  stopSession()方法被调用"); 
      //调用该方法以通知容器，移除该bean实例、终止会话。方法体可以是空的。
    }
    
    public int getDalCount() {
        return   DAOFactory.getDalDAO().getTestDalDs();
    }

    public int getOracleCount() {
        return   DAOFactory.getOracleDAO().getTestOracleDs();
    }
    
}
